package com.linsen.core.ohc.util;

import cn.hutool.core.collection.CollUtil;
import com.linsen.core.common.cache.ICache;
import com.linsen.core.common.util.LocalDateTimeUtil;
import lombok.RequiredArgsConstructor;
import org.caffinitas.ohc.OHCache;

import java.time.Duration;
import java.util.List;

/**
 * @author zhaoyong
 * @date 2023/4/11
 */
@RequiredArgsConstructor
public class OHCacheUtil<V> implements ICache<String, V> {
    private final OHCache<String, V> ohCache;

    @Override
    public V get(String key) {
        return this.ohCache.get(key);
    }

    @Override
    public boolean set(String key, V value) {
        return this.ohCache.put(key, value);
    }

    @Override
    public boolean set(String key, V value, Duration duration) {
        return this.ohCache.put(key, value, LocalDateTimeUtil.toPlusTimestamp(duration));
    }

    @Override
    public boolean hasKey(String key) {
        return this.ohCache.containsKey(key);
    }

    @Override
    public boolean del(String key) {
        return this.ohCache.remove(key);
    }

    @Override
    public List<String> keys() {
        return CollUtil.newArrayList(this.ohCache.keyIterator());
    }
}
